package view;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;

import control.TransMatrixOperations;

import model.trans_matrix.TransitionMatrix;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JToggleButton;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

public class AppWindow extends JFrame {

	/**
	 * 
	 */
	private int sideSquares = 4;
	private static final long serialVersionUID = 1262030370190587259L;
	private JPanel contentPane;
	private int windowWidth = 800;
	private int windowHeight = 600;
	private CanvasPApplet canvas;
	private JPanel panel = new JPanel();
	private JButton btnPath;
	private TransitionMatrix tranMatrix;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					if(System.getProperty("os.name").toLowerCase().indexOf("win")>=0){
			      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");						
					}
					AppWindow frame = new AppWindow();
					frame.setVisible(true);
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the frame.
	 */
	public AppWindow() {
		setTitle("hmm-maze-solver");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, windowWidth, windowHeight);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);		
		
		canvas = new CanvasPApplet(3*windowWidth/4, windowHeight - 29,sideSquares);
		canvas.init();
		contentPane.add(canvas);
		
		tranMatrix = new TransitionMatrix(sideSquares);
		
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(3*windowWidth/4, 0, 190, 571);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnResetGrid = new JButton("Reset Grid");
		btnResetGrid.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				canvas.resetGrid();
			}
		});
		btnResetGrid.setBounds(10, 11, 170, 49);
		panel.add(btnResetGrid);
		
		btnPath = new JButton("Calculate Path");
		btnPath.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TransMatrixOperations operations = new  TransMatrixOperations();
				operations.calculateTranProbabilities(tranMatrix,canvas.getGrid());
			}
		});
		btnPath.setBounds(10, 71, 170, 49);
		panel.add(btnPath);
		
		JRadioButton rdbtnGrass = new JRadioButton("Really Tall Grass");
		buttonGroup.add(rdbtnGrass);
		rdbtnGrass.setBounds(6, 524, 109, 23);
		panel.add(rdbtnGrass);
		
		JRadioButton rdbtnRocks = new JRadioButton("Rocks");
		buttonGroup.add(rdbtnRocks);
		rdbtnRocks.setBounds(6, 498, 109, 23);
		panel.add(rdbtnRocks);
		
		JRadioButton rdbtnNormalPath = new JRadioButton("Normal Path");
		buttonGroup.add(rdbtnNormalPath);
		rdbtnNormalPath.setBounds(6, 472, 109, 23);
		rdbtnNormalPath.setSelected(true);
		panel.add(rdbtnNormalPath);
		
		setLocationRelativeTo(null);
	}
}
